package com.psw.monitor.geserialport.utils;

import com.psw.monitor.geserialport.em.AlarmType;
import com.psw.monitor.geserialport.em.ChannelType;
import com.psw.monitor.geserialport.model.SerialPortMessage;

import java.math.BigDecimal;

public class MsgConverter {

    /**
     * ReportDetail detail = new ReportDetail();
     * detail.setChannelNo(Short.valueOf(s.charAt(0) + "" + s.charAt(1)));
     * detail.setChannelType(Short.valueOf(s.charAt(2) + ""));
     * detail.setStatus(Short.valueOf(s.charAt(3) + ""));
     * detail.setValue(new BigDecimal(s.substring(4, 10)));
     * detail.setAlarmType(Short.valueOf(s.charAt(10) + ""));
     * return detail;
     * 通道1  iodata0
     * 通道2  iodata1
     * 通道3  iodata2
     * 通道4  iodata3
     * 通道12  eco2
     * 通道13  tvoc
     * 通道14  adcdata0
     * 通道15  adcdata1
     * 通道16  adcdata2
     * 通道17 第一组温度
     * 通道18 第一组湿度
     * 通道19  第二组温度
     * 通道20   第二组湿度
     */
    public static String fromMsg(SerialPortMessage msg) {
        StringBuilder content = new StringBuilder();

        SerialPortMessage.ScanResponseData scanResponseData = msg.getScanresponsedata();
        /**
         * 通道1  iodata0
         * 通道2  iodata1
         * 通道3  iodata2
         * 通道4  iodata3
         **/
        content.append("01").append(ChannelType.SWITCH.getCode()).append(scanResponseData.getIoData()[0]).append("000000").append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("02").append(ChannelType.SWITCH.getCode()).append(scanResponseData.getIoData()[1]).append("000000").append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("03").append(ChannelType.SWITCH.getCode()).append(scanResponseData.getIoData()[2]).append("000000").append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("04").append(ChannelType.SWITCH.getCode()).append(scanResponseData.getIoData()[3]).append("000000").append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append(";");//5
        content.append(";");//6
        content.append(";");//7
        content.append(";");//8
        content.append(";");//9
        content.append(";");//10
        content.append(";");//11
        /**
         * 通道12  eco2
         * 通道13  tvoc
         **/
        SerialPortMessage.SGP30Data sgp30Data = scanResponseData.getSgp30Data();
        content.append("12").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06d", sgp30Data.getEco2())).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("13").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06d", sgp30Data.getTvoc())).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        /**
         *  通道14  adcdata0
         *  通道15  adcdata1
         *  通道16  adcdata2
         *  */
        content.append("14").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06d", scanResponseData.getAdcData()[0])).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("15").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06d", scanResponseData.getAdcData()[1])).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("16").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06d", msg.getRssi())).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        /**
         *  通道17 第一组温度
         *  通道18 第一组湿度
         *  通道19 第二组温度
         *  通道20 第二组湿度
         */
        content.append("17").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06.1f", scanResponseData.getTempHumidData()[0].getTemperature())).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("18").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06.1f", scanResponseData.getTempHumidData()[0].getHumidity())).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("19").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06.1f", scanResponseData.getTempHumidData()[1].getTemperature())).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append("20").append(ChannelType.SIMULATION.getCode()).append("0").append(String.format("%06.1f", scanResponseData.getTempHumidData()[1].getHumidity())).append(AlarmType.UN_ALARM_0.getCode()).append(";");
        content.append(";");//21
        content.append(";");//22
        content.append(";");//23
        content.append(";");//24
        content.append(";");//25
        content.append(";");//26
        content.append(";");//27
        content.append(";");//28
        content.append(";");//29
        content.append(";");//30
        content.append(";");//31
        content.append(";");//32
        content.append(";");//33
        return content.toString();
    }

    public static void main(String[] args){
//        String msg = "AABB70F295778C6A8ABE18A6EDDC0000D7FFFFFFCF1D2400999847455F43434D001217000000000000000000000000000000000000000000110015000000000000000000000000000000000003184446DC108A4FA6BE5A8AACED60F9CCDD";
//        SerialPortMessage message = MessageReader.readHexStr(msg);
//        System.out.println(ConvertHexStrAndStrUtils.bytesToHexStr(message.getAddr()));
        System.out.println(String.format("%06.1f",new BigDecimal("32").divide(new BigDecimal("11"),2,BigDecimal.ROUND_HALF_UP)));
    }
}
